<?php

namespace App\Http\Controllers\Upload;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Qcloud\Cos\Client;
use Storage;

/**
 * Class UploadController
 * @package App\Http\Controllers\Upload
 * @User wuqin
 * @Date 2018/3/7 0007
 * @Time 14:12
 */
class UploadController extends Controller
{
    /**
     * 方法 : index
     * @User wuqin
     * @DateTime 2018-03-07 14:12
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index()
    {
        return view("Upload.index");
    }

    /**
     * 方法 : upload
     * @User wuqin
     * @DateTime 2018-03-07 14:12
     * @param Request $request
     */
    public function upload(Request $request)
    {
        $token = $request->input('token');
        if (empty($token) || $token != env("UPLOAD_TOKEN"))
            return return_array(1, '非法操作');
        $file = $request->file();
        if (count($file) != 1)
            return return_array(1, '文件数量不正确');
        $file = current($file);
//            dd($file);
        // 文件是否上传成功
        if ($file->isValid()) {
            // 获取文件相关信息
            $originalName = $file->getClientOriginalName(); // 文件原名
            $ext = $file->getClientOriginalExtension();     // 扩展名
            $realPath = $file->getRealPath();   //临时文件的绝对路径
            $type = $file->getClientMimeType();     // image/jpeg

            // 上传文件
            $filename = date('Y-m-d-H-i-s') . '-' . uniqid() . '.' . $ext;
            // 使用我们新建的uploads本地存储空间（目录）
            $bool = Storage::disk('uploads')->put($filename, file_get_contents($realPath));
            if ($bool) {
                $cosClient = $this->qc_init();
                $res = $this->uploadbigfile($cosClient, public_path('uploads' . DIRECTORY_SEPARATOR . $filename), $filename);
                if (empty($res))
                    return return_array(1, '上传失败');
                $bool = Storage::disk('uploads')->delete($filename);
                $url = urldecode($res["Location"]);
                $url = str_replace('cos.ap-guangzhou', 'file', $url);
                return return_array(0, '上传成功', array('url' => $url));
            } else {
                return return_array(1, '上传失败');
            }
        } else {
            return return_array(1, '文件错误');
        }
    }

    /**
     * 方法 : qc_init
     * @User wuqin
     * @DateTime 2018-03-07 14:12
     * @return Client
     */
    public function qc_init()
    {
        $cosClient = new Client(array('region' => env('COS_REGION'),
            'credentials' => array(
                'secretId' => env('QC_SecretId'),
                'secretKey' => env('QC_SecretKey'))));
        return $cosClient;
    }

    /**
     * 方法 : listBuckets
     * @User wuqin
     * @DateTime 2018-03-07 14:12
     * @param $cosClient
     */
    public function listBuckets($cosClient)
    {
        try {
            $result = $cosClient->listBuckets();
            dd($result);
        } catch (\Exception $e) {
            echo "$e\n";
        }
    }

    /**
     * 方法 : createBucket
     * @User wuqin
     * @DateTime 2018-03-07 14:12
     * @param $cosClient
     */
    public function createBucket($cosClient)
    {
        try {
            $result = $cosClient->createBucket(array('Bucket' => 'shop-1253123347'));
            dd($result);
        } catch (\Exception $e) {
            echo "$e\n";
        }
    }

    /**
     * 方法 : uploadbigfile
     * @User wuqin
     * @DateTime 2018-03-07 14:12
     * @param $cosClient
     * @param $path
     * @param $filename
     * @return bool
     */
    public function uploadbigfile($cosClient, $path, $filename)
    {
        try {
            $result = $cosClient->upload(
                $bucket = 'shop-1253123347',
                $key = 'uploads' . "/" . date('Y-m-d') . "/" . 'img' . "/" . $filename,
                $body = fopen($path, 'rb'),
                $options = array(
                    "ACL" => 'private',
                    'CacheControl' => 'private',
                    'ServerSideEncryption' => 'AES256'));
            return $result;
        } catch (\Exception $e) {
            return false;
        }
    }

    /**
     * 方法 : putObject
     * @User wuqin
     * @DateTime 2018-03-07 14:12
     * @param $cosClient
     */
    public function putObject($cosClient)
    {
        try {
            $result = $cosClient->putObject(array(
                'Bucket' => 'shop-1253123347',
                'Key' => '11//32//43',
                'Body' => 'Hello World!',
                'ServerSideEncryption' => 'AES256'));
            dd($result);
        } catch (\Exception $e) {
            echo "$e\n";
        }
    }

    /**
     * 方法 : putBucketVersioning
     * @User wuqin
     * @DateTime 2018-03-07 14:12
     * @param $cosClient
     */
    public function putBucketVersioning($cosClient)
    {
        try {
            $result = $cosClient->putBucketVersioning(
                array('Bucket' => 'lewzylu02',
                    'Status' => 'Enabled')
            );
            dd($result);
        } catch (\Exception $e) {
            echo "$e\n";
        }
    }

    /**
     * 方法 : getObject
     * @User wuqin
     * @DateTime 2018-03-07 14:12
     * @param $cosClient
     */
    public function getObject($cosClient)
    {
        try {
            $result = $cosClient->getObject(array(
                'Bucket' => 'shop-1253123347',
                'Key' => '11',
                'VersionId' => '111'));
        } catch (\Exception $e) {
            echo "$e\n";
        }
    }

    /**
     * 方法 : getBucketLocation
     * @User wuqin
     * @DateTime 2018-03-07 14:12
     * @param $cosClient
     */
    public function getBucketLocation($cosClient)
    {
        try {
            $result = $cosClient->getBucketLocation(array(
                'Bucket' => 'lewzylu02',
            ));
        } catch (\Exception $e) {
            echo "$e\n";
        };
    }

    /**
     * 方法 : deleteObject
     * @User wuqin
     * @DateTime 2018-03-07 14:12
     * @param $cosClient
     */
    public function deleteObject($cosClient)
    {
        try {
            $result = $cosClient->deleteObject(array(
                'Bucket' => 'shop-1253123347',
                'Key' => '111.txt',
                'VersionId' => 'string'));
            dd($result);
        } catch (\Exception $e) {
            echo "$e\n";
        }
    }

    /**
     * 方法 : deleteObjects
     * @User wuqin
     * @DateTime 2018-03-07 14:12
     * @param $cosClient
     */
    public function deleteObjects($cosClient)
    {
        try {
            $result = $cosClient->deleteObjects(array(
                // Bucket is required
                'Bucket' => 'string',
                // Objects is required
                'Objects' => array(
                    array(
                        // Key is required
                        'Key' => 'string',
                        'VersionId' => 'string',
                    ),
                    // ... repeated
                ),
            ));
            dd($result);
        } catch (\Exception $e) {
            echo "$e\n";
        }
    }

    /**
     * 方法 : deleteBucket
     * @User wuqin
     * @DateTime 2018-03-07 14:12
     * @param $cosClient
     */
    public function deleteBucket($cosClient)
    {
        try {
            $result = $cosClient->deleteBucket(array(
                'Bucket' => 'shop-1253123347'));
            dd($result);
        } catch (\Exception $e) {
            echo "$e\n";
        }
    }

    /**
     * 方法 : headObject
     * @User wuqin
     * @DateTime 2018-03-07 14:12
     * @param $cosClient
     */
    public function headObject($cosClient)
    {
        try {
            $result = $cosClient->headObject(array(
                'Bucket' => 'shop-1253123347',
                'Key' => '11',
                'VersionId' => '111',
                'ServerSideEncryption' => 'AES256'));
            dd($result);
        } catch (\Exception $e) {
            echo "$e\n";
        }
    }

    /**
     * 方法 : headBucket
     * @User wuqin
     * @DateTime 2018-03-07 14:12
     * @param $cosClient
     */
    public function headBucket($cosClient)
    {
        try {
            $result = $cosClient->headBucket(array(
                'Bucket' => 'shop-1253123347'));
            dd($result);
        } catch (\Exception $e) {
            echo "$e\n";
        }
    }

    /**
     * 方法 : headObjects
     * @User wuqin
     * @DateTime 2018-03-07 14:12
     * @param $cosClient
     */
    public function headObjects($cosClient)
    {
        try {
            $result = $cosClient->headObject(array(
                'Bucket' => 'shop-1253123347',
                'Key' => '11'));
            dd($result);
        } catch (\Exception $e) {
            echo "$e\n";
        }
    }

    /**
     * 方法 : ListObjectVersions
     * @User wuqin
     * @DateTime 2018-03-07 14:12
     * @param $cosClient
     */
    public function ListObjectVersions($cosClient)
    {
        try {
            $result = $cosClient->ListObjectVersions(
                array('Bucket' => 'lewzylu02',
                    'Prefix' => 'test1G')
            );
            dd($result);
        } catch (\Exception $e) {
            echo "$e\n";
        }
    }

    /**
     * 方法 : listObjects
     * @User wuqin
     * @DateTime 2018-03-07 14:12
     * @param $cosClient
     */
    public function listObjects($cosClient)
    {
        try {
            $result = $cosClient->listObjects(array(
                'Bucket' => 'shop-1253123347'));
            dd($result);
        } catch (\Exception $e) {
            echo "$e\n";
        }
    }

    /**
     * 方法 : getBucketVersioning
     * @User wuqin
     * @DateTime 2018-03-07 14:12
     * @param $cosClient
     */
    public function getBucketVersioning($cosClient)
    {
        try {
            $result = $cosClient->getBucketVersioning(
                array('Bucket' => 'lewzylu02',)
            );
            dd($result);
        } catch (\Exception $e) {
            echo "$e\n";
        }
    }

    /**
     * 方法 : getObjectUrl
     * @User wuqin
     * @DateTime 2018-03-07 14:12
     * @param $cosClient
     */
    public function getObjectUrl($cosClient)
    {
        try {
            $bucket = 'shop-1253123347';
            $key = 'hello.txt';
            $url = "/{$key}";
            $request = $cosClient->get($url);
            $signedUrl = $cosClient->getObjectUrl($bucket, $key, '+10 minutes');
            echo($signedUrl);
        } catch (\Exception $e) {
            echo "$e\n";
        }
    }

    /**
     * 方法 : putBucketACL
     * @User wuqin
     * @DateTime 2018-03-07 14:12
     * @param $cosClient
     */
    public function putBucketACL($cosClient)
    {
        try {
            $result = $cosClient->PutBucketAcl(array(
                'Bucket' => 'shop-1253123347',
                'Grants' => array(
                    array(
                        'Grantee' => array(
                            'DisplayName' => 'qcs::cam::uin/327874225:uin/327874225',
                            'ID' => 'qcs::cam::uin/327874225:uin/327874225',
                            'Type' => 'CanonicalUser',
                        ),
                        'Permission' => 'FULL_CONTROL',
                    ),
                    // ... repeated
                ),
                'Owner' => array(
                    'DisplayName' => 'qcs::cam::uin/3210232098:uin/3210232098',
                    'ID' => 'qcs::cam::uin/3210232098:uin/3210232098',
                ),));
            dd($result);
        } catch (\Exception $e) {
            echo "$e\n";
        }
    }

    /**
     * 方法 : getBucketACL
     * @User wuqin
     * @DateTime 2018-03-07 14:12
     * @param $cosClient
     */
    public function getBucketACL($cosClient)
    {
        try {
            $result = $cosClient->GetBucketAcl(array(
                'Bucket' => 'shop-1253123347',));
            dd($result);
        } catch (\Exception $e) {
            echo "$e\n";
        }
    }

    /**
     * 方法 : putObjectACL
     * @User wuqin
     * @DateTime 2018-03-07 14:12
     * @param $cosClient
     */
    public function putObjectACL($cosClient)
    {
        try {
            $result = $cosClient->PutBucketAcl(array(
                'Bucket' => 'shop-1253123347',
                'Grants' => array(
                    array(
                        'Grantee' => array(
                            'DisplayName' => 'qcs::cam::uin/327874225:uin/327874225',
                            'ID' => 'qcs::cam::uin/327874225:uin/327874225',
                            'Type' => 'CanonicalUser',
                        ),
                        'Permission' => 'FULL_CONTROL',
                    ),
                    // ... repeated
                ),
                'Owner' => array(
                    'DisplayName' => 'qcs::cam::uin/3210232098:uin/3210232098',
                    'ID' => 'qcs::cam::uin/3210232098:uin/3210232098',
                ),));
            dd($result);
        } catch (\Exception $e) {
            echo "$e\n";
        }
    }

    /**
     * 方法 : getObjectACL
     * @User wuqin
     * @DateTime 2018-03-07 14:12
     * @param $cosClient
     */
    public function getObjectACL($cosClient)
    {
        try {
            $result = $cosClient->getObjectAcl(array(
                'Bucket' => 'shop-1253123347',
                'Key' => '11'));
            dd($result);
        } catch (\Exception $e) {
            echo "$e\n";
        }
    }

    /**
     * 方法 : putBucketLifecycle
     * @User wuqin
     * @DateTime 2018-03-07 14:12
     * @param $cosClient
     */
    public function putBucketLifecycle($cosClient)
    {
        try {
            $result = $cosClient->putBucketLifecycle(array(
                // Bucket is required
                'Bucket' => 'lewzylu06-1252448703',
                // Rules is required
                'Rules' => array(
                    array(
                        'Expiration' => array(
                            'Days' => 1000,
                        ),
                        'ID' => 'id1',
                        'Filter' => array(
                            'Prefix' => 'documents/'
                        ),
                        // Status is required
                        'Status' => 'Enabled',
                        'Transitions' => array(
                            array(
                                'Days' => 200,
                                'StorageClass' => 'NEARLINE'),
                        ),
                        // ... repeated
                    ),
                )));
            dd($result);
        } catch (\Exception $e) {
            echo "$e\n";
        }
    }

    /**
     * 方法 : getBucketLifecycle
     * @User wuqin
     * @DateTime 2018-03-07 14:12
     * @param $cosClient
     */
    public function getBucketLifecycle($cosClient)
    {
        try {
            $result = $cosClient->getBucketLifecycle(array(
                // Bucket is required
                'Bucket' => 'shop-1253123347',
            ));
            dd($result);
        } catch (\Exception $e) {
            echo "$e\n";
        }
    }

    /**
     * 方法 : deleteBucketLifecycle
     * @User wuqin
     * @DateTime 2018-03-07 14:12
     * @param $cosClient
     */
    public function deleteBucketLifecycle($cosClient)
    {
        try {
            $result = $cosClient->deleteBucketLifecycle(array(
                // Bucket is required
                'Bucket' => 'shop-1253123347',
            ));
            dd($result);
        } catch (\Exception $e) {
            echo "$e\n";
        }
    }

    /**
     * 方法 : putBucketCors
     * @User wuqin
     * @DateTime 2018-03-07 14:12
     * @param $cosClient
     */
    public function putBucketCors($cosClient)
    {
        try {
            $result = $cosClient->putBucketCors(array(
                // Bucket is required
                'Bucket' => 'shop-1253123347',
                // CORSRules is required
                'CORSRules' => array(
                    array(
                        'ID' => '1234',
                        'AllowedHeaders' => array('*'),
                        // AllowedMethods is required
                        'AllowedMethods' => array('PUT'),
                        // AllowedOrigins is required
                        'AllowedOrigins' => array('http://www.qq.com',),
                    ),
                    // ... repeated
                ),
            ));
            dd($result);
        } catch (\Exception $e) {
            echo "$e\n";
        }
    }

    /**
     * 方法 : getBucketCors
     * @User wuqin
     * @DateTime 2018-03-07 14:12
     * @param $cosClient
     */
    public function getBucketCors($cosClient)
    {
        try {
            $result = $cosClient->getBucketCors(array(
                // Bucket is required
                'Bucket' => 'shop-1253123347',
            ));
            dd($result);
        } catch (\Exception $e) {
            echo "$e\n";
        }
    }

    /**
     * 方法 : deleteBucketCors
     * @User wuqin
     * @DateTime 2018-03-07 14:12
     * @param $cosClient
     */
    public function deleteBucketCors($cosClient)
    {
        try {
            $result = $cosClient->deleteBucketCors(array(
                // Bucket is required
                'Bucket' => 'shop-1253123347',
            ));
            dd($result);
        } catch (\Exception $e) {
            echo "$e\n";
        }
    }

    /**
     * 方法 : copyobject
     * @User wuqin
     * @DateTime 2018-03-07 14:12
     * @param $cosClient
     */
    public function copyobject($cosClient)
    {
        try {
            $result = $cosClient->copyObject(array(
                // Bucket is required
                'Bucket' => 'lewzylu01-1252448703',
                // CopySource is required
                'CopySource' => 'lewzylu02-1252448703.cos.ap-guangzhou.myqcloud.com/test1G?versionId=MTg0NDY3NDI1NTk0MzUwNDQ1OTg',
                // Key is required
                'Key' => 'string',
                'ServerSideEncryption' => 'AES256'
            ));
            dd($result);
        } catch (\Exception $e) {
            echo "$e\n";
        }
    }

    /**
     * 方法 : Copy
     * @User wuqin
     * @DateTime 2018-03-07 14:12
     * @param $cosClient
     */
    public function Copy($cosClient)
    {
        try {
            $result = $cosClient->Copy($bucket = 'lewzylu01-1252448703',
                $key = 'string',
                $copysource = 'lewzylu02-1252448703.cos.ap-guangzhou.myqcloud.com/test1G',
                $options = array('VersionId' => 'MTg0NDY3NDI1NTk0MzUwNDQ1OTg',
                    'ServerSideEncryption' => 'AES256'));
            dd($result);
        } catch (\Exception $e) {
            echo "$e\n";
        }
    }

    /**
     * 方法 : restoreObject
     * @User wuqin
     * @DateTime 2018-03-07 14:12
     * @param $cosClient
     */
    public function restoreObject($cosClient)
    {
        try {
            $result = $cosClient->restoreObject(array(
                // Bucket is required
                'Bucket' => 'lewzylu02',
                // Objects is required
                'Key' => '11',
                'Days' => 7,
                'CASJobParameters' => array(
                    'Tier' => 'Bulk'
                )
            ));
            dd($result);
        } catch (\Exception $e) {
            echo "$e\n";
        }
    }
}
